const fs = require('fs');
const xlsx = require('node-xlsx');
const cheerio = require('cheerio');
const axios = require('axios');

// 可选
const requestHeaders = {
  ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
  Referer: 'http://money.163.com/'
}

class Crawler {
  constructor(option) {
    this.pageUrl = option.pageUrl;
    this.cachePath = option.cachePath || './cache.html';
    this.exportExcelPath = option.exportExcelPath || 'report.xlsx';
  }

  async start () {
    let html = await this.readLocalCache()
    // let html = ''
    if (!html) {
      html = await this.request();
    }
    const parseResult = this.processPageHTML(html)
    // console.log('parseResult', parseResult)
    this.exportExcel(parseResult)
  }

  async request () {
    return axios.get(this.pageUrl, {
      headers: requestHeaders
    }).then(res => {
      this.createLocalCache(res.data)
      return Promise.resolve(res.data)
    }).catch(err => {
      console.log('err')
      return Promise.reject(err)
    })
  }
  // 读本地缓存
  async readLocalCache () {
    this.checkFileExist(this.cachePath);
    return fs.readFileSync(this.cachePath, 'utf-8')
  }

  // 创建本地缓存
  createLocalCache (html = '') {
    console.log('createLocalCache', html)
    this.checkFileExist(this.cachePath);
    return fs.writeFileSync(this.cachePath, html, 'utf-8')
  }
  checkFileExist(filePath) {
    try {
      console.log('存在', filePath)
      fs.statSync(filePath);
      return true;
    }catch (err) {
      console.log('不存在文件', filePath)
      fs.writeFileSync(filePath, '', 'utf-8');
      return false
    }
  }

  processPageHTML (html) {
    const result = { title: [], rate: [] }
    const $ = cheerio.load(html)

    const $thead = $('.table_bg001.border_box.limit_sale') // 类名之间以.链接起来，不能为空格
    const nameTd = $thead.find('tbody tr:nth-child(11)').children()
    console.log('name', $(nameTd).text())
    // 存入名称
    result.title.push('名称')
    result.rate.push($(nameTd[0]).text().trim())

    const $table = $('.table_bg001.border_box.limit_sale.scr_table');
    const tableTitle = $table.find('tbody tr:first-child').children();
    
    const rateTr = $table.find('tbody tr:nth-child(12)')
    const rateTds = rateTr.children()
    // console.log('tableTitle', tableTitle)
    for (let i = 0; i < tableTitle.length; i++) {
      const titleTd = tableTitle[i]
      console.log('titleTd', titleTd)
      result.title.push($(titleTd).text().trim());
      const rateTd = rateTds[i]
      result.rate.push($(rateTd).text().trim())
    }

    return result;
  }

  exportExcel (dataObj) {
    const values = Object.values(dataObj);   
    console.log('values', values)
    const excelBuffer = xlsx.build([{name: '茅台', data: values }])
    // xlsx文件不存在 会报错，进入catch
    try {
      fs.statSync(this.exportExcelPath)
    } catch {
      fs.writeFileSync(this.exportExcelPath, excelBuffer, 'buffer');
      console.log('导出')
    }
    // if (!fs.statSync(this.exportExcelPath)) {
    //   fs.writeFileSync(this.exportExcelPath, excelBuffer, 'buffer');
    //   console.log('导出')
    // }
    console.log('已存在了')
  }
}

const crawler = new Crawler({ pageUrl: 'http://quotes.money.163.com/f10/zycwzb_600519,report.html' })
crawler.start();